package com.hjy.coupon.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.hjy.common.to.MemberPrice;
import com.hjy.common.to.SpuReductionTo;
import com.hjy.coupon.dao.MemberPriceDao;
import com.hjy.coupon.dao.SkuLadderDao;
import com.hjy.coupon.entity.MemberPriceEntity;
import com.hjy.coupon.entity.SkuLadderEntity;
import com.hjy.coupon.service.MemberPriceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hjy.common.utils.PageUtils;
import com.hjy.common.utils.Query;

import com.hjy.coupon.dao.SkuFullReductionDao;
import com.hjy.coupon.entity.SkuFullReductionEntity;
import com.hjy.coupon.service.SkuFullReductionService;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Autowired
    private SkuLadderDao skuLadderDao;

    @Autowired
    private MemberPriceService memberPriceDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveReduction(SpuReductionTo spuReductionTo) {
        // 1. sms_sku_ladder 阶梯价格
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        BeanUtil.copyProperties(spuReductionTo,skuLadderEntity);
        skuLadderEntity.setAddOther(spuReductionTo.getCountStatus());
        if (skuLadderEntity.getFullCount() > 0) {
            skuLadderDao.insert(skuLadderEntity);
        }


        // 2. sms_sku_full_reduction
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        BeanUtil.copyProperties(spuReductionTo,skuFullReductionEntity);
        skuFullReductionEntity.setAddOther(spuReductionTo.getCountStatus());
        if (skuFullReductionEntity.getFullPrice().compareTo(new BigDecimal(0))>0) {
            baseMapper.insert(skuFullReductionEntity);
        }


        // 3. sms_member_price
        List<MemberPrice> memberPrices = spuReductionTo.getMemberPrice();
        if (CollUtil.isNotEmpty(memberPrices)) {
            List<MemberPriceEntity> collect = memberPrices.stream().map(memberPrice -> {
                MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
                memberPriceEntity.setSkuId(spuReductionTo.getSkuId());
                memberPriceEntity.setMemberLevelName(memberPrice.getName());
                memberPriceEntity.setMemberLevelId(memberPrice.getId());
                memberPriceEntity.setMemberPrice(memberPrice.getPrice());
                memberPriceEntity.setAddOther(0);
                return memberPriceEntity;
            }).filter(memberPriceEntity -> {
                return memberPriceEntity.getMemberPrice().compareTo(new BigDecimal("0")) > 0;
            }).collect(Collectors.toList());

            memberPriceDao.saveBatch(collect);
        }

    }

}